import 'package:flutter/material.dart';

class InfoRow extends StatelessWidget {
  InfoRowType infoRowType;
  String title;
  String value;
  BuildContext context;
  bool enable = true;
  bool textFieldCanEdit;
  Function onPress;
  ListTile _listTile;
  TextFormField _textField;
  int maxLines;
  TextAlign textAlign;
  double height;
  double padding = 5;

  MyOrientation orientation;

  TextEditingController textEditingController;

   Function onGetValue;

  InfoRow(this.title,

      {this.infoRowType = InfoRowType.TEXTFIELD,
      this.textFieldCanEdit = true,
      this.value,
      this.enable = true,
      this.maxLines = 1,
      this.textAlign = TextAlign.end,
      this.onPress,
      this.orientation = MyOrientation.horizontal,
      this.padding,
      this.textEditingController,
      this.height = 40,
        this.onGetValue,
      })
      : super();

  @override
  Widget build(BuildContext context) {
    print('InfoRow build');
    print('$value');
    textFieldCanEdit = enable;
    if (infoRowType == InfoRowType.TEXT) {
      textFieldCanEdit = false;
    }

//    if (textEditingController == null) {
//      textEditingController = TextEditingController();
//    }
//    textEditingController.text = value.isEmpty ? '' : value;

//    _textField = TextField(
//      controller: textEditingController,
//      textAlign: textAlign,
//      maxLines: maxLines,
//      style: TextStyle(fontSize: 15, color: Colors.black54),
//      enabled: textFieldCanEdit,
//      decoration: InputDecoration(
//          enabled: textFieldCanEdit,
//          labelStyle: TextStyle(color: Colors.black87),
//          border: InputBorder.none),
//      onChanged: (value) {
//        textEditingController.text = value;
//        this.value = value;
//      },
//    );

    if (textEditingController  == null) {
      _textField = TextFormField(
        textAlign: textAlign,
        initialValue:  value,
        maxLines: maxLines,
        style: TextStyle(fontSize: 15, color: Colors.black54),
        enabled: textFieldCanEdit,
        decoration: InputDecoration(
            labelStyle: TextStyle(color: Colors.black87),
            border: InputBorder.none),
        onSaved: (value) {
          this.value = value;
          if (onGetValue != null) {
            onGetValue(value);
          }
        },
      );
    }else {
      _textField = TextFormField(
        controller: textEditingController,
        textAlign: textAlign,
        maxLines: maxLines,
        style: TextStyle(fontSize: 15, color: Colors.black54),
        enabled: textFieldCanEdit,
        decoration: InputDecoration(
            labelStyle: TextStyle(color: Colors.black87),
            border: InputBorder.none),
        onSaved: (value) {

          this.value = value;
          if (onGetValue != null) {
            onGetValue(value);
          }

        },
      );
    }

    Widget item;
    if (orientation == MyOrientation.horizontal) {
      item = Row(
        children: <Widget>[
          Text(
            title,
            style: TextStyle(fontSize: 15),
          ),
          Expanded(
            child: _textField,
            flex: 1,
          )
        ],
      );
    } else {
      item = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            title,
            style: TextStyle(fontSize: 15),
          ),
          _textField
        ],
      );
    }

    _listTile = ListTile(
      onTap: enable && infoRowType == InfoRowType.TEXT ?  onPress : null,
      title: item,
      trailing: enable && infoRowType == InfoRowType.TEXT
          ? Icon(
              Icons.chevron_right,
              color: Theme.of(context).primaryColor,
            )
          : null,
    );
    return Padding(
      padding: EdgeInsets.only(top: 1),
      child: Container(
        height: height,
        color: Colors.white,
        child: Center(
          child: _listTile,
        ),
      ),
    );
  }
}

enum InfoRowType {
  TEXTFIELD,
  TEXT,
}

enum MyOrientation { vertical, horizontal }
